package log;

import java.time.LocalDateTime;

import org.slf4j.helpers.MessageFormatter;

public class StringFormatPerfTest {

    private static final int COUNT = 1000000;

    private static final String JDK_FORMAT = "now time is %s";
    private static final String SLF4J_FORMAT = "now time is {}";

    public static void main(String[] args) throws Exception {
        while (true) {
            run();
        }
    }

    private static String format(String format, Object... args) {
        return MessageFormatter.arrayFormat(format, args).getMessage();
    }

    private static void run() {
        String now = LocalDateTime.now().toString();
        long start = System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
            format(SLF4J_FORMAT, now);
        }
        long end = System.currentTimeMillis();
        long slf4jTime = end - start;

        start = System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
            String.format(JDK_FORMAT, now);
        }
        end = System.currentTimeMillis();
        long jdkTime = end - start;

        start = System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
            String str = "now time is " + now;
        }
        end = System.currentTimeMillis();
        long time = end - start;

        System.out.println(time + " " + slf4jTime + " " + jdkTime);

    }

}
